<template>
  <div>
    <el-table :data="orders">
      <el-table-column prop="orderId" label="订单编号"></el-table-column>
      <el-table-column prop="userId" label="用户ID"></el-table-column>
      <el-table-column prop="shopName" label="门店名称"></el-table-column>
      <el-table-column prop="orderPrice" label="总价"></el-table-column>
      <el-table-column prop="createdAt" label="创建时间"></el-table-column>
      <el-table-column prop="updatedAt" label="更新时间"></el-table-column>
      <el-table-column prop="isPayed" label="是否付款">
        <template slot-scope="scope">
          <span v-if="scope.row.isPayed === 0">未支付</span>
          <span v-if="scope.row.isPayed === 1">已支付</span>
          <span v-if="scope.row.isPayed === 2">已确认</span>
          <span v-if="scope.row.isPayed === 3">已退款</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isPayed === 1" type="success" @click="pay(scope.row.orderId)">确认订单</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isPayed !== 3" type="danger" @click="reject(scope.row.orderId)">取消订单</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axios from "axios";
import {getSessionStorage} from "@/common";
import qs from "qs";

export default {
  name: "OrderList",
  data(){
    return{
      user: {},
      orders: []
    }
  },
  created() {
    this.user = getSessionStorage('user')
    axios.post('/order/listOrdersByBusinessId', qs.stringify({
      businessId: this.user.businessId
    })).then(response => {
      this.orders = response.data
      console.log(this.orders)
    })
  },
  methods:{
    pay(orderId){
      axios.post('/order/receiveOrder', qs.stringify({
        orderId: orderId
      })).then(response => {
        alert('订单确认成功')
        location.reload()
      })
    },
    reject(orderId){
      axios.post('/order/rejectOrder', qs.stringify({
        orderId: orderId
      })).then(response => {
        alert('订单取消成功')
        location.reload()
      })
    }
  }
}
</script>

<style scoped>

</style>